package com.yztc.orangeentertainment.ui.login.phonelogin;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.yztc.orangeentertainment.App;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.bean.UserBean;
import com.yztc.orangeentertainment.ui.main.MainActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/21.
 */
public class PhoneLoginActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.userName_et)
    EditText userNameEt;
    @InjectView(R.id.userName_pwd)
    EditText userNamePwd;
    @InjectView(R.id.login_btn)
    Button loginBtn;
    @InjectView(R.id.register_btn)
    Button registerBtn;
    @InjectView(R.id.iv_anim)
    ImageView ivAnim;
    private AnimationDrawable drawable;
    @Override
    public int getLayoutId() {
        return R.layout.activity_login_phone;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:
                final String id = userNameEt.getText().toString();
                final String password = userNamePwd.getText().toString();
                final UserBean userBean = App.getLiteOrm().queryById(id, UserBean.class);
                if (userBean == null) {
                    Toast.makeText(PhoneLoginActivity.this, "用户不存在", Toast.LENGTH_LONG).show();
                } else {
                    if (!password.equals(userBean.getPsw())) {
                        Toast.makeText(PhoneLoginActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                    } else {
                        App.killActivity(MainActivity.class);
                        Intent intent = new Intent(PhoneLoginActivity.this, MainActivity.class);
                        intent.putExtra("id", id);
                        intent.putExtra("tag", 1);
                        startActivity(intent);
                        finish();
                    }
                }
                break;
            case R.id.register_btn:
                startActivity(new Intent(PhoneLoginActivity.this, PhoneRegisterActivity.class));
                finish();
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
        App.addActivity(this);
        initView();
    }

    private void initView() {
        loginBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
        drawable= (AnimationDrawable) ivAnim.getDrawable();
        ivAnim.postDelayed(new Runnable() {
            @Override
            public void run() {
               drawable.start();
            }
        },500);
    }





}
